package cn.com.nou.base.service.bdm;

import cn.com.nou.base.dao.bdm.ResourceClassificationMapper;
import cn.com.nou.common.builder.PaginationBuilder;
import cn.com.nou.common.entity.NouEnumerationT;
import cn.com.nou.common.entity.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class ResourceClassificationImpl implements ResourceClassificationService {


    @Autowired
    ResourceClassificationMapper classificationMapper;

    /**
     * 添加资源分类
     *
     * @param nouEnumerationT
     * @return
     */

    @Override
    public int addResourceClassification(NouEnumerationT nouEnumerationT) throws Exception {
        try {
            return classificationMapper.addResourceClassification(nouEnumerationT);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 判断是否存在相同资源分类代码或资源分类名称
     *
     * @param itemCode
     * @param itemName
     * @return
     */
    @Override
    public boolean getResourceClassificationListCount(String itemCode, String itemName) throws Exception {
        try {
            NouEnumerationT nouEnumerationT = classificationMapper.getResourceClassificationListCount(itemCode, itemName);
            if (nouEnumerationT != null) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据id查询判断status是否为0
     *
     * @param id
     * @return
     */
    @Override
    public boolean getUsedResourceClassificationCount(String id) throws Exception {
        try {
            NouEnumerationT nouEnumerationT = classificationMapper.getUsedResourceClassificationCount(id);
            if (nouEnumerationT != null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int deleteResourceClassification(String id) throws Exception {
        try {
            return classificationMapper.deleteResourceClassification(id);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public int updateResourceClassification(NouEnumerationT nouEnumerationT) throws Exception {
        try {
            return classificationMapper.updateResourceClassification(nouEnumerationT);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public Pagination getResourceClassificationList(NouEnumerationT nouEnumerationT,int pageNo,int pageSize) throws Exception {
        try {
            PaginationBuilder builder = new PaginationBuilder();
            Pagination p = builder.getPaginating(pageNo, pageSize);
            List<NouEnumerationT> nouEnumerationT1 = classificationMapper.getResourceClassificationList
                    (nouEnumerationT, p.getOffset(), p.getLimit());
            p.setResultList(nouEnumerationT1);
            p.setPageNo(pageNo);
            p.setPageSize(pageSize);
            return p;
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
    }

}
